<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
				xmlns:instance="http://www.ics.uci.edu/pub/arch/xArch/instance.xsd"
				xmlns:types="http://www.ics.uci.edu/pub/arch/xArch/types.xsd"
				xmlns:xlink="http://www.w3.org/1999/xlink">

	<xsl:output indent="yes"/>

	<xsl:template match="text()"/>

	<xsl:template match="/">
		<xsl:apply-templates select="instance:xArch"/>
	</xsl:template>

	<xsl:template match="instance:xArch">
		<xsl:element name="xArch">
			<xsl:apply-templates select="types:archStructure"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="types:archStructure">
		<xsl:element name="archStructure">
			<xsl:attribute name="name">
				<xsl:value-of select="types:description/text()"/>
			</xsl:attribute>
			<xsl:attribute name="id">
				<xsl:value-of select="types:component/@types:id"/>
			</xsl:attribute>

			<!--
			<xsl:element name="components">
				<xsl:for-each select="types:component">
					<xsl:element name="component">
						<xsl:attribute name="name">
							<xsl:value-of select="types:description/text()"/>
						</xsl:attribute>
						<xsl:attribute name="id">
							<xsl:value-of select="@types:id"/>
						</xsl:attribute>
						<xsl:for-each select="types:interface">
							<xsl:element name="interface">
								<xsl:attribute name="id">
									<xsl:value-of select="@types:id"/>
								</xsl:attribute>
								<xsl:attribute name="type">
									<xsl:value-of select="types:direction/text()"/>
								</xsl:attribute>
							</xsl:element>
						</xsl:for-each>
					</xsl:element>
				</xsl:for-each>
			</xsl:element>
			-->

			<!--
			<xsl:element name="connectors">
				<xsl:for-each select="types:connector">
					<xsl:element name="connector">
						<xsl:attribute name="name">
							<xsl:value-of select="types:description/text()"/>
						</xsl:attribute>
						<xsl:attribute name="id">
							<xsl:value-of select="@types:id"/>
						</xsl:attribute>
						<xsl:for-each select="types:interface">
							<xsl:element name="interface">
								<xsl:attribute name="id">
									<xsl:value-of select="@types:id"/>
								</xsl:attribute>
								<xsl:attribute name="type">
									<xsl:value-of select="types:direction/text()"/>
								</xsl:attribute>
							</xsl:element>
						</xsl:for-each>
					</xsl:element>
				</xsl:for-each>
			</xsl:element>
			-->

			<xsl:element name="paths">
				<xsl:for-each select="types:component">
					<xsl:for-each select="types:interface[types:direction/text() != 'in']">
						<xsl:element name="path">
							<xsl:call-template name="createObject">
								<xsl:with-param name="name" select="'src'"/>
								<xsl:with-param name="obj" select=".."/>
							</xsl:call-template>

							<xsl:variable name="firstSrcIfId" select="concat('#', @types:id)"/>
							<xsl:variable name="firstLink" select="//types:link[.//instance:anchorOnInterface/@xlink:href = $firstSrcIfId]"/>
							<!--
							<xsl:call-template name="createObject">
								<xsl:with-param name="name" select="'link'"/>
								<xsl:with-param name="obj" select="$firstLink"/>
							</xsl:call-template>
							-->

							<xsl:variable name="firstDestIfId" select="substring($firstLink//instance:anchorOnInterface[@xlink:href != $firstSrcIfId]/@xlink:href, 2)"/>
							<xsl:variable name="con" select="//types:connector[types:interface/@types:id = $firstDestIfId]"/>
							<!--
							<xsl:call-template name="createObject">
								<xsl:with-param name="name" select="'via'"/>
								<xsl:with-param name="obj" select="$con"/>
							</xsl:call-template>
							-->

							<xsl:for-each select="$con/types:interface[@types:id != $firstDestIfId and ./types:direction/text() != 'in']">
								<xsl:variable name="secondSrcIfId" select="concat('#', ./@types:id)"/>
								<xsl:variable name="secondLink" select="//types:link[.//instance:anchorOnInterface/@xlink:href = $secondSrcIfId]"/>
								<!--
								<xsl:call-template name="createObject">
									<xsl:with-param name="name" select="'link'"/>
									<xsl:with-param name="obj" select="$secondLink"/>
								</xsl:call-template>
								-->

								<xsl:variable name="secondDestIfId" select="substring($secondLink//instance:anchorOnInterface/@xlink:href[. != $secondSrcIfId], 2)"/>
								<xsl:variable name="dest" select="//types:component[types:interface/@types:id = $secondDestIfId]"/>
								<xsl:call-template name="createObject">
									<xsl:with-param name="name" select="'dest'"/>
									<xsl:with-param name="obj" select="$dest"/>
								</xsl:call-template>
							</xsl:for-each>
						</xsl:element>
					</xsl:for-each>
				</xsl:for-each>
			</xsl:element>

		</xsl:element>
	</xsl:template>

	<xsl:template name="createObject">
		<xsl:param name="name"/>
		<xsl:param name="obj"/>
		<xsl:element name="{$name}">
			<xsl:attribute name="name">
				<xsl:value-of select="$obj/types:description/text()"/>
			</xsl:attribute>
			<xsl:attribute name="id">
				<xsl:value-of select="$obj/@types:id"/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>
